import { ECSEntity } from "../../../3rd/ECS/ECSEntity";
import { ECSSystem } from "../../../3rd/ECS/ECSSystem";

import { BindNode } from "../../../Bridge/BindNode";
import { MathBirde } from "../../../Bridge/Math";
import { Vector3 } from "../../../Bridge/Vec";
import { App } from "../../../Framework/Core/App";
import NodeHelper from "../../../Bridge/NodeHelper";
import Util from "../../../Framework/Util/Util";
import { GameRT } from "../../GameRT";
import { MovableComponent } from "../components/MovableComponent";
import { PlayerComponent, StarComponent } from "../components/NoDataComponent";
import { RenderNodeComponent } from "../components/RenderNodeComponent";
import { BackStartPoint } from "../events/GameEvent";
import { PositionComponent } from "../components/PositionComponent";

/**
 * 可以移动的方向
 */

export class StarFactorSystem extends ECSSystem {


    private readonly starRoot:BindNode;



    /**
     * 构造函数
     *
     * @param renderNode 放置 NPC 渲染节点的容器
     * @param goInst NPC Prefab
     */
    constructor() {
        super();
        this.starRoot = NodeHelper.FindBindNode("starRoot")
 
    }

    start(): void {
        this.removeStars();
        this.bornStars();    
    }

    update(dt: number): void {
        
        var backPoint = this.ecs.events.fetch(BackStartPoint)
        backPoint.forEach(element => {
            this.removeStars();
            this.bornStars();    
        });
       
    }

    bornStars()
    {
        Util.Log("bornStars");

       
        //   = MathBirde.();
        var num = MathBirde.RandRange(GameRT.ScenLength/2,GameRT.ScenLength/4);

        for (let index = 0; index <num; index++) {
            var starEty = new ECSEntity("star"+index);
            var star =  App.Instance.poolMgr.Get("star");
            starEty.addComponent(new RenderNodeComponent(star));
            starEty.addComponent(new StarComponent());
            var leftOrRight = GameRT.RoadSpace * Math.ceil( MathBirde.RandRange(-1,1) );
            var randomPos = new Vector3(leftOrRight,0,MathBirde.RandRange(0,GameRT.ScenLength));
            starEty.addComponent(new PositionComponent(randomPos)); 
            this.ecs.entities.add(starEty);           
        }

    }

    removeStars()
    {
        // var starsCom =  this.ecs.components.all(StarComponent)
        // starsCom.forEach(element => {
          
        //     var enity = this.ecs.entities.get(element.entityId);
        //     var goWarp = enity.getComponent(RenderNodeComponent).node
        //     goWarp.Put(); //回收节点对象
        //     this.ecs.entities.delete(element.entityId)
        // });

        var starsComs =  this.ecs.components.all(StarComponent)
    
        var removeIds:string[]=[];
        starsComs.forEach(element => {  
            removeIds.push(element.entityId);
        });

        removeIds.forEach(entityId=>{
            var enity = this.ecs.entities.get(entityId);
            var goWarp = enity.getComponent(RenderNodeComponent).node
            goWarp.Put(); //回收节点对象
            // Util.Log("回收",barrierComs.length,"障碍物")
            this.ecs.entities.delete(enity)
        })

    }

    //// private

    // private createNPC() {
    //     // 创建一个 NPC 的渲染节点
    //     // const NPCNode = UnityEngine.GameObject.Instantiate(this.goInst.gameObject) as UnityEngine.GameObject;

    //     // NPCNode.setParent(this.renderNode);
    //     const NPCNode = this.goInst.Instantiate(this.goInst);
    //     NPCNode.SetParent(this.renderNode)

    

    //     // 创建一个 NPC 实体，并添加需要的组件
    //     const npc = new ECSEntity();
    //     // 添加 NPC 组件
    //     npc.addComponent(new NPCComponent());
    //     // 添加渲染节点组件，并指定 npcNode
    //     npc.addComponent(new RenderNodeComponent(NPCNode));
    //     // 添加健康度组件，并指定初始 HP
    //     // npc.addComponent(new HealthComponent(100, barNode));
    //     // 添加移动组件，并指定初始位置、移动方向和速度
    //     const direction =
    //         DIRECTIONS[Math.floor(Math.random() * DIRECTIONS.length)];
    //     const speed = Math.random() * 100 + 10;
    //     npc.addComponent(new MovableComponent(new Vec3(),  speed));

    //     // 将实体添加到 ECS 中
    //     this.ecs.entities.add(npc);
    // }
}
